<?PHP
session_start();
include("../config/connect.php");
include("../public/common.php");
include("../public/biaoqing.php");


if( !isset($_SESSION["email"]) ) 
{
	$url = "Location:login.php?ref=edit_items.php";
	header($url);
}

$userid = $_SESSION["userid"];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑商品</title>
</head>
<body>
<?php
$id = CheckUrlParamNoDie("id");
$opt = CheckUrlParamNoDie("opt");
$itemid = "";
$itemname = "";
$systag = "";
$tag = "";
$fee = 0;
$commission = 0;
$url = "";
$picurl = "";
$desc = "";

if( $id != "" )
{
	$sql = "select * from goods_items where id=$id limit 1";
	$result = mysql_query( $sql );
	if( $result && $row = mysql_fetch_assoc( $result ) )
	{
		$itemid = $row["num_iid"];
		$itemname = $row["item_title"];
		$systag = $row["sys_tag"];
		$tag = $row["tag"];
		$fee = $row["fee"];
		$commission = $row["commission"];
		$url = $row["url"];
		$picurl = $row["pic_url"];
		$desc = $row["r_desc"];
	}
}

if( $opt == "save" && isset( $_POST["submit"] ))
{
	$itemid = GetPostParamStr("itemid");
	$itemname = GetPostParamStr("itemname");
	$systag = trim(GetPostParamStr("systag"));
	$tag = " ".trim(GetPostParamStr("tag"))." ";
	$fee = GetPostParamStr("fee");
	$commission = GetPostParamStr("commission");
	$url = GetPostParamStr("url");
	$picurl = GetPostParamStr("picurl");
	$desc = GetPostParamStr("desc");

	if ($id == "")	// insert
	{
		$sql = "insert into goods_items (num_iid, item_title, fee, commission, pic_url, url, user_id, sys_tag, tag, r_desc, state) values ('$itemid', '$itemname', $fee, $commission, '$picurl', '$url', $userid, '$systag', '$tag', '$desc', 3)";
	}
	else	// update
	{
		$sql = "update goods_items set item_title='$itemname', fee=$fee, commission=$commission, pic_url='$picurl', url='$url', sys_tag='$systag', tag='$tag', r_desc='$desc' where id=$id";
	}

	mysql_query( $sql );
	if (mysql_affected_rows() == 0)
	{
		if ($id == "")
			echo "<script>alert('插入失败，请找程序员。');</script>";
		else
			echo "<script>alert('没有条目被更新，如有疑问，请找程序员。');</script>";
	}
	else
	{
		if ($id == "")
			echo "<script>alert('添加成功!');window.history.go(-2);</script>";
		else
			echo "<script>alert('编辑成功!');window.history.go(-2);</script>";
	}
}

?>
<div align="center" class="STYLE1">添加/编辑商品后台 <span class="STYLE2">当前用户：</span><?PHP echo $_SESSION["email"]; ?></div>
<br />
<form id="frm" action="?id=<?PHP echo $id; ?>&opt=save" method="post" onsubmit="javascript:return OnSubmit()">
<table cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td width="90" height="30" align="center">商品URL：</td>
        <td >
        <input type="text" name="itemurl" id="itemurl" value="" size="70" />
        <span id="ishasitem"></span>
        <input type="hidden" name="itemid" id="itemid" value="<?PHP echo $itemid; ?>" />
        <input type="hidden" size="70" name="url" id="url" height="20" value="<?PHP echo $url; ?>"  />
        <input type="button" onclick="OnSync()" value="获取数据" /></td>
    </tr>
    <tr>
   	  <td width="90" height="30" align="center">名称：</td>
      <td height="30" ><input type="text" size="70" name="itemname" id="itemname" height="20" value="<?PHP echo $itemname; ?>"  /></td>
    </tr>
    <tr>
    	<td  height="30" align="center"s>系统标签：</td>
        <td width="622" ><input type="text" name="systag" id="systag" size="30" value="<?PHP echo $systag; ?>">
          &nbsp;<span style="font-size:12px">请选择系统分类，注意：如果选择不对，将无法展示商品</span></td>
    </tr>
     <tr>
    	<td  height="30" align="center"s>标签：</td>
        <td width="622" ><input type="text" size="70" name="tag" id="tag" height="20" value="<?PHP echo $tag; ?>"  />          &nbsp;</td>
    </tr>
    <tr>
    	<td  height="30"  align="center"s>金额：</td>
        <td ><input type="text" size="70" name="fee" id="fee" height="20" value="<?PHP echo $fee; ?>"  />          &nbsp;</td>
    </tr>
    <tr>
    	<td  height="30" align="center"s>返现：</td>
        <td ><input type="text" size="70" name="commission" id="commission" height="20" value="<?PHP echo $commission; ?>"  />          &nbsp;</td>
    </tr>
    <tr>
    	<td  height="30" align="center"s>图片：</td>
        <td ><input type="text" size="70" name="picurl" id="picurl" height="20" value="<?PHP echo $picurl; ?>"  /></td>
    </tr>
    <tr>
    	<td  height="30" align="right"s>推荐语：</td>
        <td ><textarea name="desc" id="desc" cols="56" rows="7"><?PHP echo $desc; ?></textarea>
          <br />
        <?PHP echo ShowBiaoQing("OnBiaoQing", 20); ?>        
        </td>
    </tr>
    <tr>
    	<td colspan="6" height="30" align="center"><input type="submit" name="submit" value="保存" style=" width:80px; height:23px" /></td>
    </tr>
</table>
</form>

<p align="center"><img id="pic" <?PHP if ($picurl!="") echo "src='".$picurl."'"; ?>/></p>
</body>
</html>

<script language="javascript" src="../js/jquery.min.js"></script>
<script language="javascript">
function gup(id) {
	var dz_url = document.getElementById("itemurl").value;
    id = id.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+id+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec(dz_url);
    if( results == null ) {
        return "";
    } else {
        return results[1];
    }
}

function OnSync()
{
	//var itemid = document.getElementById("itemid").value;
//	alert(itemid);
	if(gup("id") != ""){
		var url_id = gup("id");  //个人 商城
	}
	if(gup("item_num_id") != "" ){
		var url_id = gup("item_num_id");  //部分话旗舰店
	}	
	if(gup("item_id") != "" ){
		var url_id = gup("item_id");  //// 旅游、酒店
	}
	if(gup("mallstItemId") != "" ){
		var url_id = gup("mallstItemId");  //// 淘宝商城官方旗舰店
	}
	
	document.getElementById("itemid").value = url_id;

	var url = "http://www.tusijie.com/admin/find_item.php?itemid=" + url_id;
	var res = $.ajax({	url: url,
			async: false,
		}).responseText;
//	alert(res);
	var resObj = eval('(' + res + ')');
	if (resObj.code == 1)
		document.getElementById("ishasitem").innerHTML="<font color='#FF0000'>已经有相同的商品哦</font>";
	else if (resObj.code == 0)
		document.getElementById("ishasitem").innerHTML="";
	else
		alert("判断是否存在商品出错！请找冰峰");
			
	url = "http://www.tusijie.com/admin/get_item_info.php?itemid=" + url_id;
	res = $.ajax({	url: url,
			async: false,
		}).responseText;
//	alert(res);
	var resObj = eval('(' + res + ')');
	
//	alert(resObj.tag);
	if (resObj.code == 1)
	{
		document.getElementById("url").value=resObj.click_url ? resObj.click_url : resObj.detail_url;
		document.getElementById("itemname").value=resObj.title;
		document.getElementById("tag").value=resObj.tag;
		document.getElementById("picurl").value=resObj.pic_url;
		document.getElementById("fee").value=resObj.price;
		document.getElementById("commission").value=resObj.commission ? resObj.commission : 0;
		document.getElementById("pic").src=resObj.pic_url ? resObj.pic_url : "";
	}
}

function OnSubmit()
{
	if (document.getElementById("itemid").value=="" ||
		document.getElementById("systag").value=="" ||
		document.getElementById("tag").value=="" ||
		document.getElementById("url").value=="" ||
		document.getElementById("itemname").value=="" ||
		document.getElementById("picurl").value=="" ||
		document.getElementById("fee").value=="" ||
		document.getElementById("commission").value=="")
	{
		alert("输入内容不能为空！");
		return false;
	}

	return true;
//	document.getElementById("frm").submit();
}

function OnBiaoQing(str)
{
	var obj = document.getElementById("desc");
    var ubbLength=obj.value.length; 
    obj.focus(); 
    if(typeof document.selection !="undefined") 
		document.selection.createRange().text=str; 
    else 
		obj.value=obj.value.substr(0,obj.selectionStart)+str+obj.value.substring(obj.selectionStart,ubbLength); 
}
</script>